package com.xpec.c4.h5net;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.xpec.c4.message.login.CAccountLoginRequest;

public class ObjectByte {
	public static byte[] objectToBytes(Object obj) {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oout = null;
		byte[] returnByte = null;
		try {
			oout = new ObjectOutputStream(bout);
			oout.writeObject(obj);
			if (oout != null) {
				oout.close();
			}
			bout.close();
			returnByte = bout.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return returnByte;
	}
	
	public static Object bytesToObject(byte[] bytes) {
		ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
		ObjectInputStream oin = null;
		Object returnObject = null;
		try {
			oin = new ObjectInputStream(bin);
			if (oin != null) {
				oin.close();
			}
			bin.close();
			returnObject = oin.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return returnObject;
	}
	
	public static void main(String[] args) {
		CAccountLoginRequest request = new CAccountLoginRequest();
		request.clientIp = "123";
		request.code = "456";
		request.login = "789";
		request.loginPlatForm = "987";
		request.reconnectToken = "321";
//		String request = "123";
//		int request = 123;
		byte[] abc = objectToBytes(request);
		Object bcd = bytesToObject(abc);
	}

}